import { Line2 } from 'three/addons/lines/Line2.js';
import { BaseModel, Params } from './baseModel.js';
import { WORDS } from './WORDS.js';
import { Helper } from '../helper.js';
import { LineMaterial } from 'three/addons/lines/LineMaterial.js';

interface DataLineParams extends Params {
    width: number,
    color: string,
    points: [number, number, number][],
}

class DataLine {
    id: string
    obj: Line2
    type: string
    platformIndex?: number
    params: DataLineParams
    constructor(obj: Line2, params: DataLineParams) {
        if (!obj.isMesh) {
            throw new Error(WORDS.ERROR.NOT_LINE2);
        }

        if (params.id) {
            this.id = params.id
        } else {
            this.id = Date.now().toString()
            params.id = this.id
        }

        this.params = params
        this.obj = obj
        this.obj.name = this.id
        this.type = WORDS.OBJNAME.MODEL_DATALINE
        this.obj.userData.type = this.type
    }

    setOpacity(val: number) {
        (this.obj.material as LineMaterial).opacity = val
        this.params.opacity = val
    }

    updateParams() {
        const mtl = this.obj.material as LineMaterial
        this.params.color = Helper.getHexColorStr(mtl.color)
        this.params.width = mtl.linewidth
        this.params.opacity = mtl.opacity
    }

    toJSON() {
        this.updateParams()
        return this.params
    }
}

export { DataLine };
export type { DataLineParams };
